<?php

include_once("../Process_Data/process_data.php");

function api_input(&$info_struct) {

    $api = POST('select_api');
    if ($api === "DOI") {
        $info_struct->doi = POST('api');
        $refs = process_doi($info_struct);
    } else if ($api === "ArXiv") {
        $info_struct->arxiv = POST('api');
        $refs = process_arxiv($info_struct);
    } else if ($api === "ZMath") {
        $info_struct->zmath = POST('api');
        $refs = process_zmath($info_struct);
    }

    return $refs;

}

function manual_input(&$info_struct) {

    if (isset($_POST['title']))
        $info_struct->title = POST('title');
    if (isset($_POST['year']))
        $info_struct->year = POST('year');
    if (isset($_POST['author']))
        $info_struct->author = POST('author');

    if (isset($_POST['note']))
        $info_struct->note = POST('note');

    if (isset($_POST['doi']))
        $info_struct->doi = POST('doi');
    if (isset($_POST['zmath']))
        $info_struct->zmath = POST('zmath');
    if (isset($_POST['arxiv']))
        $info_struct->arxiv = POST('arxiv');
    if (isset($_POST['number']))
        $info_struct->number = POST('number');
    if (isset($_POST['volume']))
        $info_struct->volume = POST('volume');
    if (isset($_POST['journal']))
        $info_struct->journal = POST('journal');
    if (isset($_POST['pages']))
        $info_struct->pages = POST('pages');
    if (isset($_POST['month']))
        $info_struct->month = POST('month');
    if (isset($_POST['other']))
        $info_struct->other = POST('other');
    if (isset($_POST['bc']) && POST('bc') === "yes")
        $info_struct->bc = true;

    $info_struct->from = "man";

    if ($info_struct->title !== "" && $info_struct->author!== "" && $info_struct->year !== "") {
        $refs = process_manual_data($info_struct);
    } else {
        $refs = NULL;
    }

    return $refs;
}

function bibtex_input(&$info_struct) {

    include_once('bibtexParse/PARSEENTRIES.php');

    $parse = new PARSEENTRIES();
    $parse->loadBibtexString(POST('bib_in'));
    $parse->extractEntries();

    list($preamble, $strings, $entries, $undefinedStrings) = $parse->returnArrays();

    if (isset($entries[0]) && is_array($entries[0])) {
        if(array_key_exists("title", $entries[0])){
            $info_struct->title = $entries[0]["title"];
        }
        if(array_key_exists("author", $entries[0])){
            $info_struct->author = $entries[0]["author"];
        }
        if(array_key_exists("ajournal", $entries[0])){
            $info_struct->journal = $entries[0]["journal"];
        }
        if(array_key_exists("year", $entries[0])){
            $info_struct->year = $entries[0]["year"];
        }
        if(array_key_exists ( "volume" , $entries[0])){
            $info_struct->volume = $entries[0]["volume"];
        }
        if(array_key_exists ( "number" , $entries[0])){
            $info_struct->number = $entries[0]["number"];
        }
        if(array_key_exists ( "pages" , $entries[0])){
            $info_struct->pages = $entries[0]["pages"];
        }
        if(array_key_exists ( "month" , $entries[0])){
            $info_struct->month = $entries[0]["month"];
        }
        if(array_key_exists ( "other" , $entries[0])){
            $info_struct->other = $entries[0]["other"];
        }
    }

    if (!isset($info_struct->title) || !isset($info_struct->author) || !isset($info_struct->year)) {
        $refs = NULL;
    } else {
        $refs = process_bibtex($info_struct);
    }

    return $refs;
}

?>